﻿using DbEntityTools.Model;
using DbEntityTools.Service;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace DbEntityTools.Config
{
    public partial class DbLogin : Form
    {
        public MySqlConnectionService service = null;
        public DbConfigModel dbModel = new DbConfigModel();


        public DbLogin()
        {
            InitializeComponent();
        }

        private void DbLogin_Load(object sender, EventArgs e)
        {

        }

        private void OkBtn_Click(object sender, EventArgs e)
        {
            try
            {
                CheckDbConfig();
                service = new MySqlConnectionService(dbModel.GetConnectionStr());
                var isOk = service.TestConnection();
                if (isOk)
                {
                    this.DialogResult = DialogResult.OK;
                }
            }
            catch (Exception)
            {
                MessageBox.Show("连接数据库失败!");
            }
        }

        private void TestConnBut_Click(object sender, EventArgs e)
        {
            try
            {
                CheckDbConfig();
                service = new MySqlConnectionService(dbModel.GetConnectionStr());
                var isOk = service.TestConnection();
                if (isOk)
                {
                    MessageBox.Show("连接数据库成功!");
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void CancaleBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private bool CheckDbConfig()
        {
            if (string.IsNullOrWhiteSpace(DbServcerText.Text))
            {
                MessageBox.Show("数据库服务器不可为空", "提示");
                DbServcerText.Focus();
                return false;
            }
            dbModel.DbServer = DbServcerText.Text.Trim();
            if (string.IsNullOrWhiteSpace(DbNameText.Text))
            {
                dbModel.DbName = dbModel.DbServer;
            }
            if (string.IsNullOrWhiteSpace(UidText.Text))
            {
                MessageBox.Show("用户名不可为空", "提示");
                DbServcerText.Focus();
                return false;
            }
            dbModel.UId = UidText.Text.Trim();
            if (string.IsNullOrWhiteSpace(PasswordText.Text))
            {
                MessageBox.Show("密码不可为空", "提示");
                DbServcerText.Focus();
                return false;
            }
            dbModel.Password = PasswordText.Text.Trim();
            if (string.IsNullOrWhiteSpace(DbPortText.Text))
            {
                MessageBox.Show("端口不可为空", "提示");
                DbServcerText.Focus();
                return false;
            }
            try
            {
                dbModel.Port = Convert.ToInt32(DbPortText.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("端口格式不正确", "提示");
                DbServcerText.Focus();
                return false;
            }
            return true;
        }
    }
}
